package com.yunji.framework_template.biz.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.yunji.framework_template.biz.cache.CacheService;
import com.yunji.framework_template.common.web.Page;
import com.yunji.framework_template.orm.persistence.mapper.SitesInfoMapper;
import com.yunji.framework_template.orm.persistence.model.SitesInfo;

/**
 * Created by fenglibin on 2018/11/15.
 */
@Service
public class SitesInfoService {

    @Autowired
    private SitesInfoMapper sitesInfoMapper;

    @Autowired
    private CacheService    cacheService;

    @Value("#{settings['countryTopSites']}")
    private int             countryTopSites;
    @Value("#{settings['excludeFromAllCountries']}")
    private String          excludeFromAllCountries;

    public int deleteByPrimaryKey(Integer id) {
        return sitesInfoMapper.deleteByPrimaryKey(id);
    }

    public int insert(SitesInfo record) {
        return sitesInfoMapper.insert(record);
    }

    public int insertSelective(SitesInfo record) {
        return sitesInfoMapper.insertSelective(record);
    }

    public SitesInfo selectByPrimaryKey(Integer id) {
        SitesInfo sitesInfo = cacheService.getCache().getSitesInfo().get(id);
        if (sitesInfo != null) {
            return sitesInfo;
        }
        sitesInfo = sitesInfoMapper.selectByPrimaryKey(id);
        if (sitesInfo != null) {
            cacheService.getCache().getSitesInfo().put(id, sitesInfo);
        }
        return sitesInfo;
    }

    public int updateByPrimaryKeySelective(SitesInfo record) {
        return sitesInfoMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(SitesInfo record) {
        return sitesInfoMapper.updateByPrimaryKey(record);
    }

    /**
     * 更新访问次数
     * 
     * @param record
     * @return
     */
    public int updateVisitsTime(SitesInfo record) {
        return sitesInfoMapper.updateVisitsTime(record);
    }

    public List<SitesInfo> selectByCondition(SitesInfo record) {
        return sitesInfoMapper.selectByCondition(record);
    }

    public List<SitesInfo> selectAll() {
        return sitesInfoMapper.selectAll();
    }

    public List<SitesInfo> selectByPage(Page<SitesInfo> page) {
        return sitesInfoMapper.selectByPage(page);
    }

    public Integer count(SitesInfo record) {
        return sitesInfoMapper.count(record);
    }

    public int deleteByCondition(SitesInfo record) {
        return sitesInfoMapper.deleteByCondition(record);
    }

    List<SitesInfo> selectCountryTopSites() {
        return sitesInfoMapper.selectTopSites();
    }

    List<SitesInfo> selectWorldTopSites() {
        return sitesInfoMapper.selectWorldTopSites();
    }

    List<SitesInfo> selectTopByType(Map param) {
        return sitesInfoMapper.selectTopByType(param);
    }

    List<SitesInfo> selectRandomTopByType(Map param) {
        return sitesInfoMapper.selectRandomTopByType(param);
    }

    /**
     * 从所有的国家中获取导航网站，每个国家选择一定的数量
     * 
     * @return
     */
    public List<SitesInfo> getSitesInfoFromAllCountries() {
        List<SitesInfo> topSites = new ArrayList<SitesInfo>();
        String[] countryArr = cacheService.getCountries().split(",");
        for (String countryCode : countryArr) {
            if (excludeFromAllCountries.indexOf(countryCode) >= 0) {
                continue;
            }
            List<SitesInfo> currentTopSites = cacheService.getCache(countryCode).getTopSites();
            if (currentTopSites == null || currentTopSites.size() == 0) {
                continue;
            }
            for (int index = 0; index < currentTopSites.size(); index++) {
                if (index >= countryTopSites) {
                    break;
                }
                topSites.add(currentTopSites.get(index));
            }
        }
        return topSites;
    }

}
